package com.mindskip.xzs.controller.data;

import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.ExamPaper;
import com.mindskip.xzs.domain.Question;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.service.ExamPaperService;
import com.mindskip.xzs.service.QuestionService;
import com.mindskip.xzs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 数据中心接口，
 */
@RestController
public class data {
    /**
     * @Description: 引入依赖
     */
    @Autowired
    UserService userService;
    @Autowired
    ExamPaperService examPaperService;
    @Autowired
    QuestionService questionService;


    @GetMapping("/api/data")
    public RestResponse getData(String  name){
        //装总的返回数据
        ArrayList<ArrayList<Integer>> list=new ArrayList<>();

        ArrayList<Integer> yuefenCount=new ArrayList<>();
        //1先获取到各年级学生数量
        ArrayList<Integer> UserCount=new ArrayList<>();
        List<User> users = userService.getUsers();
        int[] counts=new int[100];
        int[] yuefen=new int[13];//只要1到12
        for (User user : users) {
            //admin是不在任何等级的
           if(user.getUserLevel()!=null){
               counts[user.getUserLevel()]=counts[user.getUserLevel()]+1;
           }

            //2找每个月注册的人数
            Date createTime = user.getCreateTime();
            //获取月份--这个月份要加1
            int month = createTime.getMonth()+1;
            System.out.println(month);
            yuefen[month]=yuefen[month]+1;
        }
        //只要1到7
        for (int i = 1; i < 8; i++) {
            UserCount.add(counts[i]);
        }
        //装入list中
        list.add(UserCount);

        //2找各月的注册人数
        //只要1-12
        for (int i1=1;i1<13;i1++){
            yuefenCount.add(yuefen[i1]);
        }
        list.add(yuefenCount);


        //3找各年级试卷数量
        int[] examCount=new int[100];
        List<ExamPaper> examPapers = examPaperService.selectAll();
        if(examPapers!=null){
            for (ExamPaper examPaper : examPapers) {
                if(examPaper.getGradeLevel()!=null){
                    //那么添加到等级
                    examCount[examPaper.getGradeLevel()]=examCount[examPaper.getGradeLevel()]+1;
                }
            }
            //找出来的数据填充
            ArrayList<Integer> temp=new ArrayList<>();
            for(int i=1;i<8;i++){
                temp.add(examCount[i]);
            }
            //添加各年级试卷数量
            list.add(temp);
        }

        //4试卷总数+试卷占比
        Integer total = examPaperService.selectAllCount();
        //添加到list中
        ArrayList<Integer> temp06=new ArrayList<>();
        temp06 .add(total);
        list.add(temp06);
        //计算占比--这里怕试卷为0
        ArrayList<Integer> temp01=new ArrayList<>();
        for(int i=1;i<8;i++){
            System.out.println(examCount[i]);
            double d=examCount[i];
            double total01=total;
            double result=d/total01;
            System.out.println(result);
            temp01.add((int)(result*100));
        }
        //添加到list中
        list.add(temp01);

        //计算各年级的题目数量
        List<Question> questions = questionService.selectAll();
        int[] questionCount=new int[100];
        if(questions!=null){
            for (Question question : questions) {
                if(question.getGradeLevel()!=null){
                    //那么添加到等级
                    questionCount[question.getGradeLevel()]=questionCount[question.getGradeLevel()]+1;
                }
            }
            //找出来的数据填充
            ArrayList<Integer> temp03=new ArrayList<>();
            for(int i4=1;i4<8;i4++){
                temp03.add(questionCount[i4]);
            }
            //添加到list中
            list.add(temp03);
        }

        return RestResponse.ok(list);
    }
}
